package com.tanhua.server.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.common.enums.LogTypeEnum;
import com.tanhua.server.service.LogMQService;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("movements")
public class QuanZiController {
    @Autowired
    private QuanZiService quanZiService;

    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * 查询好友动态
     * @param page
     * @param pageSize
     * @param token
     * @return
     */
    @GetMapping
    public PageResult queryPublishList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize,
                                       @RequestHeader("Authorization")String token){
        return quanZiService.queryPublishList(page,pageSize,token);
    }


    /**
     * 发布动态
     * @param textContent
     * @param location
     * @param latitude
     * @param longitude
     * @param multipartFiles
     * @return
     */
    @Autowired
    private LogMQService logMQService;
    @PostMapping
    public ResponseEntity<Void> savePublish(@RequestParam("textContent")String textContent,
                                            @RequestParam(value = "location",required = false)String location,
                                            @RequestParam(value = "latitude",required = false)String latitude,
                                            @RequestParam(value = "longitude",required = false)String longitude,
                                            @RequestParam(value = "imageContent",required = false)MultipartFile[] multipartFiles){
        try {
            String publishId=quanZiService.savePublish(textContent,location,latitude,longitude,multipartFiles);
            if(StrUtil.isNotEmpty(publishId)){
                //发动态
                this.logMQService.movementsAdd(LogTypeEnum.MOVEMENTS_ADD.getValue());
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询推荐动态，返回的是封装好的PageResult对象
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public PageResult queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.quanZiService.queryRecommendPublishList(page, pageSize);
    }


    /**
     * 点赞，@PathVariable是用来获得请求url中的动态参数的
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount=quanZiService.likeComment(publishId);
            if(likeCount!=null){
                //TODO 发送消息
                this.quanziMQService.likePublishMsg(publishId);
                this.logMQService.movementsLike(LogTypeEnum.MOVEMENTS_LIKE.getValue());

                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 取消点赞，@PathVariable是用来获得请求url中的动态参数的
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id")String publishId){
        try {
            Long disLikeCount=quanZiService.disLikeComment(publishId);
            if(disLikeCount!=null){
                //TODO 发消息
                this.quanziMQService.disLikePublishMsg(publishId);
                this.logMQService.movementsUnlike(LogTypeEnum.MOVEMENTS_UNLIKE.getValue());

                return ResponseEntity.ok(disLikeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }




    /**
     * 喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount=this.quanZiService.loveComment(publishId);
            if(null!=loveCount){
                //发消息
                this.quanziMQService.lovePublishMsg(publishId);
                this.logMQService.movementsLove(LogTypeEnum.MOVEMENTS_LOVE.getValue());
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> disLoveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount=this.quanZiService.disLoveComment(publishId);
            if(null!=loveCount){
                //TODO 发消息
                this.quanziMQService.disLovePublishMsg(publishId);
                this.logMQService.movementsUnLove(LogTypeEnum.MOVEMENTS_UNLOVE.getValue());
                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询单条动态信息
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<QuanZiVo> queryById(@PathVariable("id") String publishId) {
        try {
            QuanZiVo movements = this.quanZiService.queryById(publishId);
            if(null != movements){
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            List<VisitorsVo> list = quanZiService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 自己的所有动态
     *
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.quanZiService.queryAlbumList(userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}
